//
//  ST_MatchingAnimationViewController.swift
//  btvideo
//
//  Created by mac on 2020/7/8.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyJSON

class ST_MatchingAnimationViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title_nav_btn.setTitleForAllStates(self.title ?? "")
        
        self.view.insertSubview(back_image_view, belowSubview: self.own_navigation_bar)
        
        
        view.addSubview(own_navigation_bar)
        own_navigation_bar.snp.makeConstraints { (make) in
            make.right.left.top.equalTo(view)
            make.height.equalTo(statusBarHeight+44)
        }
        own_navigation_bar.addSubview(left_nav_btn)
        left_nav_btn.snp.makeConstraints { (make) in
            make.left.equalTo(own_navigation_bar).offset(8)
            make.bottom.equalTo(own_navigation_bar)
            make.width.height.equalTo(44)
        }
        
        own_navigation_bar.addSubview(title_nav_btn)
        title_nav_btn.snp.makeConstraints { (make) in
            make.centerY.equalTo(left_nav_btn)
            make.centerX.equalTo(own_navigation_bar)
        }
        
        back_image_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.view.addSubview(first_animation_view)
        first_animation_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(35)
            make.right.equalToSuperview().offset(-35)
            make.height.equalTo(first_animation_view.snp.width)
            make.top.equalToSuperview().offset((104-20)/375.0*ScreenWidth+statusBarHeight)
        }
        
        self.view.addSubview(match_title_label)
        match_title_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.height.equalTo(28)
            make.top.equalTo(first_animation_view.snp.bottom).offset(82)
        }
        
        self.view.addSubview(match_content_label)
        match_content_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.height.equalTo(40)
            make.top.equalTo(match_title_label.snp.bottom).offset(10)
        }
        
            
        self.first_animation_view.play { (f) in
            
            self.first_animation_view.isHidden = true

            self.view.addSubview(self.second_animation_view)
            self.second_animation_view.snp.makeConstraints { (make) in
                make.edges.equalTo(self.first_animation_view)
            }
            self.second_animation_view.play()
        }
        
        if self.title?.contains("闪") ?? false{
            request_flash_game_data()
        }else{
            request_bump_data()
        }
    }
    
    var supplementId = ""
    
    func request_flash_game_data()  {
        
        JVrequest(PH.quickTalkApi,parameters: ["conf_id":supplementId],success: { (json) in
            
            
            let dataJson = json["data"]
            
            print("闪聊:",dataJson)
            
            if dataJson.dictionaryValue.keys.count < 1{
                return
            }
            
            main_thread_execute(afterDelay: 2) {
                
//                BTAppBaseDataOfLogin.share.expendForTalk(gameId: self.supplementId, freeTime: dataJson["free_num"].intValue)
                
                ST_BTUserInfoManager.shared.modify_sweet_coin(value: dataJson["sweet_currency"].floatValue)
                
                self.searched_user_id = dataJson["accid"].stringValue
             
                
                BINP.update_base_info(remoteInfo: ST_BTUserInfoFromRemote.init(accid: json["accid"].stringValue, avatar: json["avatar"].stringValue, userName: json["nickname"].stringValue))
                
                let vc = ST_PrivateChatViewController()
                
                vc.sessionID = dataJson["accid"].stringValue
                
                vc.other_chat_source = 2
                
                vc.source = "闪聊匹配"
                
                vc.dismiss_oper = true
                
                self.navigationController?.pushViewController(vc)
                
                
                
            }
            
        }) { (code, err) in
            
            self.toast(text: err)
        }
    }
    
    
    func request_bump_data()  {
        
        JVrequest(PH.bumpApi,parameters: ["conf_id":supplementId], success: { (json) in
            
            let data = json["data"]
            
            print("特质对对碰:",data)
            
            
            
            if data.dictionaryValue.keys.count < 1{
                return
            }
            
            self.searched_user_id = data["user"]["accid"].stringValue
            
            main_thread_execute(afterDelay: 2) {
                
//                BTAppBaseDataOfLogin.share.expendForTalk(gameId: self.supplementId, freeTime: data["free_num"].intValue)
                
                ST_BTUserInfoManager.shared.modify_sweet_coin(value: data["sweet_currency"].floatValue)
                
                let vc = ST_AIMatchSuccessViewController()
                
                vc.json = data
                
                self.navigationController?.pushViewController(vc, animated: false)
                
            }
            
        }) { (code, err) in
            
            self.toast(text: err)
        }
        
    }
    

//     MARK: - Proeprties
    var matchSetting:(matchTitle:String,content:String)?{
        didSet{
            guard let s = self.matchSetting else{
                return
            }
            match_title_label.text = s.matchTitle
            match_content_label.text = s.content
        }
    }
    
    fileprivate lazy var own_navigation_bar:UIView = {
       let control = UIView()
        control.backgroundColor = .clear
        return control
    }()
    
    
    fileprivate var searched_user_id:String?
    
    
    fileprivate lazy var title_nav_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleColorForAllStates(.white)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.semibold)
//        control.addTarget(self, action: #selector(title_click), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    fileprivate lazy var left_nav_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
        control.setImage(ST_ImageUtils.imageName("路径_8"), for: UIControl.State.normal)
        
        control.imageView?.contentMode = UIView.ContentMode.scaleAspectFit
        
        control.addTarget(self, action: #selector(left_click), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    @objc fileprivate func left_click(){
        
        if let userId = self.searched_user_id{
            
            JVrequest(PH.unlikePersonApi,parameters:["target_accid":userId],success: { (json) in
                    
                print("碰一碰不喜欢成功")
            }) { (code, err) in
                
                print("碰一碰不喜欢失败:",err)
            }
        }
        
        self.goback_previous_controller(animated: false)
    }
    
    fileprivate lazy var back_image_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("碰一碰背景"))
        return control
    }()
    
    fileprivate lazy var first_animation_view:LOTAnimationView = {
        
        guard  let path = Bundle.main.path(forResource: "supperzzleZoom", ofType: "json") else{
             return LOTAnimationView()
         }
        let animationControl = LOTAnimationView.init(filePath: path)
        
        animationControl.contentMode = UIView.ContentMode.scaleAspectFill
        return animationControl
    }()
    
    fileprivate lazy var second_animation_view:LOTAnimationView = {
        
        guard  let path = Bundle.main.path(forResource: "supperzzleZoomSeconds", ofType: "json") else{
             return LOTAnimationView()
         }
        let animationControl = LOTAnimationView.init(filePath: path)
        
        animationControl.contentMode = UIView.ContentMode.scaleAspectFill
        
        animationControl.loopAnimation = true
        
        return animationControl
    }()
    
    
    fileprivate lazy var match_title_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = .white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.semibold)
        return control
    }()

    
    fileprivate lazy var match_content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = .white
        control.numberOfLines = 2
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()

    
}
